<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="cn.school.util.StringKit"%>
<%@page import="java.util.UUID"%>
<%@page import="java.io.File"%>
<%@page import="java.util.Date"%>
<%@page import="cn.school.entity.Student"%>
<%@page import="cn.school.service.Service"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% 
	Service s=new Service();
	DiskFileItemFactory factory = new DiskFileItemFactory();
	factory.setSizeThreshold(10*1024 * 1024);
	ServletFileUpload upload = new ServletFileUpload(factory);
	upload.setHeaderEncoding("utf-8");
	List<FileItem> list = upload.parseRequest(request);
  	Student stu=new Student();
  	String imgName="";
  	String newimgName="";
  	String inputName="";
  	int stuno=0;
	for(FileItem f:list){
%>
 <p>
  <% 
  	inputName=f.getFieldName();//表单元素的name属性值
	imgName=f.getName();//上传文件的文件名，普通表单为null
  if(f.isFormField()) {
    if(inputName.equals("stuno")){
    	stuno=StringKit.toInt(f.getString("utf-8"));
	  	stu.setStuno(stuno);
	}else if(inputName.equals("sname")){
  		stu.setSname(f.getString("utf-8"));
  	}else if(inputName.equals("sex")){
  		stu.setSex(f.getString("utf-8"));
  	}else if(inputName.equals("telephone")){
  		//telephone=f.getString("utf-8");
  		stu.setTelephone(f.getString("utf-8"));
  	}else if(inputName.equals("qq")){
  		stu.setQq(f.getString("utf-8"));
  	}else if(inputName.equals("wechat")){
  		stu.setWechat(f.getString("utf-8"));
  	}else if(inputName.equals("gradeid")){
  		stu.setGradeid(StringKit.toLong(f.getString("utf-8")));
  	}else if(inputName.equals("enterdate")){
  	 stu.setEnterdate(StringKit.toDate(f.getString("utf-8")));
  	}else if(inputName.equals("fromcity")){
  		stu.setFromcity(f.getString("utf-8"));
  	}else if(inputName.equals("birthday")){
  		stu.setBirthday(StringKit.toDate(f.getString("utf-8")));
  	}else if(inputName.equals("address")){
  		stu.setAddress(f.getString("utf-8"));
  	}else if(inputName.equals("pass")){
  		stu.setPass(f.getString("utf-8"));
  	}
  }else{
    if(f.getSize()==0)
    	continue;
	Student stu1=s.getOne(stuno);
	/*  String suffix=imgName.substring(imgName.lastIndexOf("."));
	newimgName=UUID.randomUUID() + suffix; */
  	File path=new File(application.getRealPath("img"));
	  if(path.exists()==false)
		  path.mkdirs();	
	  File file=new File(path,stu1.getIdcardpic());
	try {
		f.write(file);
		stu.setIdcardpic(stu1.getIdcardpic()); 
	} catch (Exception e) {
		out.print("文件太大，上传失败！");
	}
  }
}
  %>
<%
int r=s.update(stu);
out.print(r>0?"更新成功":"更新失败");
%>
</body>
</html>